/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package databaseproject;
import java.sql.*;
import java.util.Scanner;
/**
 *
 * @author chris
 */
public class DatabaseProject {

    /**
     * @param args the command line arguments
     */
    private static boolean userIsAdmin;
    private static String username;
    private static boolean userIsLoggedIn;
    
    static Connection con; 
    static Statement st;
    static ResultSet set;
    static int updateQuery;
    static String sqlQuery;
       
    public static void main(String[] args) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
                
        String dbDriver = "org.apache.derby.jdbc.ClientDriver";
        String connectURL = "jdbc:derby://localhost:1527/COSC457";
        
        Class.forName(dbDriver).newInstance();       
        System.out.println(dbDriver + " has been loaded.");
        con = DriverManager.getConnection(connectURL);
        System.out.println(connectURL + " has been loaded.");
        st = con.createStatement();
        boolean userQuitting = false;
        userIsLoggedIn = false;
        while (!userIsLoggedIn && !userQuitting)
        {
            Menu loginMenu = new Menu();
            loginMenu.setTitle("Log In");
            loginMenu.addOption("Log in as User");
            loginMenu.addOption("Log in as Administrator");
            loginMenu.addQuitOption();
            loginMenu.removeBackOption();

            switch (loginMenu.display())
            {
                case (1):
                    Menu chooseUser = new Menu();
                    chooseUser.addOptionsFromSQLQuery(con,
                            "select username from APP.\"USER\"");
                    chooseUser.addOption("Create User Account");
                    int choice = chooseUser.display();
                    if (choice == chooseUser.getBackOption())
                        break;
                    else if (choice == chooseUser.getBackOption() - 1)
                        username = UserCreate();
                    else
                        username = chooseUser.getOptionAt(choice);
                    userIsAdmin = false;
                    userIsLoggedIn = true;
                    doMainMenu();
                    break;
                case (2):
                    userIsAdmin = true;
                    username = "Admin";
                    userIsLoggedIn = true;
                    doMainMenu();
                    break;
                default:
                    st.close();
                    System.out.println(st + " has been closed.");
                    con.close();
                    System.out.println(con + " has been closed.");
                    System.exit(0);
            }
        }
    }
    
    public static void doMainMenu()
    {
        if (userIsAdmin)
            System.out.println("\nUser is administrator.");
        else
            System.out.printf("\nUser logged in as %s.\n", username);
        
        boolean done = false;
        while (!done)
        {
            Menu mainMenu = new Menu();
            mainMenu.addOption("Fantasy Shows");
            mainMenu.addOption("Performances");
            mainMenu.addOption("Musicians");
            mainMenu.addOption("Bands");
            mainMenu.addOption("Songs");
            mainMenu.addOption("Albums");
            mainMenu.addOption("Venues");
            mainMenu.addOption("User Info");
            


            switch (mainMenu.display())
            {
                case (1):
                    Fantasy.doMenu(con, username, userIsAdmin);
                    break;
                case (2):
                    Performance.doMenu(con, username, userIsAdmin);
                    break;    
                case (3):
                    Musician.doMenu(con, username, userIsAdmin);
                    break;
                case (4):
                    Band.doMenu(con, username, userIsAdmin);
                    break;
                case (5):
                    Song.doMenu(con, username, userIsAdmin);
                    break;
                case (6):
                    Album.doMenu(con, username, userIsAdmin);
                    break;
                case (7):
                    Venue.doMenu(con, username, userIsAdmin);
                    break;
                case (8):
                    if(User.doMenu(con, username, userIsAdmin))
                    {
                        break;
                    }
                    else
                    {
                        done = true;
                        break;
                    }
                default:
                    done = true;
                    break;
            }
        }        
        
        userIsLoggedIn = false;
    
    }
    
    public static String UserCreate()
    {
        boolean done = false;
        while(!done){     
            Scanner input = new Scanner(System.in);
            System.out.print("\nCreate A Username: ");
            String uname = input.nextLine();
            System.out.print("Insert Name: ");
            String name = input.nextLine();
            System.out.print("Birthdate- Year: ");
            int year = input.nextInt();
            System.out.print("          Month: ");
            int month = input.nextInt();
            System.out.print("            Day: ");
            int day = input.nextInt();
            String date = String.format("%04d-%02d-%02d", year, month, day);

            try
            {
                PreparedStatement st1 = con.prepareStatement("insert into \"USER\" values (?,?,?)");
                st1.setString(1, uname);
                st1.setString(2, name);
                st1.setString(3, date);
                st1.executeUpdate();
                return uname;
            }
            catch (java.sql.SQLException e)
            {
                System.out.println("This Username Is Already Taken, Try Again.");
            }
        }
        return null;
    }
}

